var wxpay = require('../../utils/pay.js')
var app = getApp()
Page({
  data:{
    statusType:["全部","待付款","待发货","待收货","待评价"],
    currentType:0,
    tabClass: ["", "", "", "", ""],
    loadingMoreHidden:false,
    currPage:1,
  },
  statusTap:function(e){
     var curType =  e.currentTarget.dataset.index;
     this.data.currentType = curType
     this.setData({
      currentType:curType,
      currPage:1
     });
     this.onShow();
  },
  orderDetail : function (e) {
    var orderId = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: "/pages/order-details/index?id=" + orderId
    })
  },
  pingjia:function(e){
    console.log("评价")
    // console.log(e);
    var oid = e.target.dataset.oid;
    var osn = e.target.dataset.osn;
    console.log(oid);
    wx.navigateTo({
      url: "/pages/pingjia/index?oid=" + oid + "&osn=" + osn
    })
  },
  cancelOrderTap:function(e){
    var that = this;
    var orderId = e.currentTarget.dataset.id;
     wx.showModal({
      title: '确定要取消该订单吗？',
      content: '',
      success: function(res) {
        if (res.confirm) {
          wx.showLoading();
          wx.request({
            url: app.globalData.host + '/api/order/CancelOrder',
            data: {
              uid: app.globalData.userInfo.Uid,
              oid: orderId
            },
            success: (res) => {
              wx.hideLoading();
              if (res.data.Code == 0) {
                that.onShow();
              }else{
                wx.showModal({
                  title: '提示',
                  content: res.data.Message,
                  showCancel:false
                })
              }
            }
          })
        }
      }
    })
  },
  toPayTap:function(e){
    var orderId = e.currentTarget.dataset.id;
    wxpay.wxpay(app, orderId, "/pages/order-list/index");
  },
  onLoad:function(e){
    // 生命周期函数--监听页面加载
    console.log("加载")
    console.log(e);
    if(e.t){
      this.setData({
        currentType:e.t
      })
    }
   
  },
  onReady:function(){
    // 生命周期函数--监听页面初次渲染完成
 
  },
  getOrderStatistics : function () {
    var that = this;
    wx.request({
      url:  app.globalData.host + '/api/order/statistics',
      data: { uid: app.globalData.userInfo.Uid },
      success: (res) => {
        wx.hideLoading();
        if (res.data.Code == 0) {
          var tabClass = that.data.tabClass;
          if (res.data.Data.count_id_no_pay > 0) {
            tabClass[1] = "red-dot"
          }
          if (res.data.Data.count_id_no_transfer > 0) {
            tabClass[2] = "red-dot"
          }
          if (res.data.Data.count_id_no_confirm > 0) {
            tabClass[3] = "red-dot"
          }
          if (res.data.Data.count_id_success > 0) {
            tabClass[4] = "red-dot"
          }

          that.setData({
            tabClass: tabClass,
          });
        }
      }
    })
  },
  onShow:function(){
    // 获取订单列表
    wx.showLoading();
    var that = this;
    that.getOrderStatistics();
    var postData = {
      uid: app.globalData.userInfo.Uid,
      orderState:0,
      page: that.data.currPage
    };
    if (that.data.currentType == 1) {
      postData.orderState = 20
    }
    if (that.data.currentType == 2) {
      postData.orderState = 80
    }
    if (that.data.currentType == 3) {
      postData.orderState = 100
    }
    if (that.data.currentType == 4) {
      postData.orderState = 120
    }
    this.getOrderStatistics();
    wx.request({
      url: app.globalData.host + '/api/order/GetUserOrders',
      data: postData,
      success: (res) => {
        wx.hideLoading();
        if (res.data.Code == 0) {
          var orderList = res.data.Data.OrderList;
          for(var i=0;i<orderList.length;i++){
            for (var j = 0; j < orderList[i].ProductInfos.length;j++){
              if (orderList[i].ProductInfos[j]){
                orderList[i].ProductInfos[j].ShowImg = app.globalData.image190_190host + orderList[i].ProductInfos[j].ShowImg;
              }
             
            }
          }
          if (that.data.currPage != 1) {
            var preGoods = that.data.orderList
            orderList = preGoods.concat(orderList);
          }
          that.setData({
            orderList: orderList,
            loadingMoreHidden: res.data.Data.PageModel.HasNextPage
          });
        } else {
          this.setData({
            orderList: null
          });
        }
      }
    })
    
  },
  loadMore: function (e) {
    var that = this;
    if (!that.data.loadingMoreHidden) return;
    var currPage = that.data.currPage;
    that.setData({
      currPage: currPage + 1
    })
    that.onShow();
  },
  confirmBtnTap: function (e) {
    var that = this;
    var orderId = e.currentTarget.dataset.id;
    wx.showModal({
      title: '确认您已收到商品？',
      content: '',
      success: function (res) {
        if (res.confirm) {
          wx.showLoading();
          wx.request({
            url: app.globalData.host + '/api/order/ReceiveOrder',
            data: {
              uid: app.globalData.userInfo.Uid,
              oid: orderId
            },
            success: (res) => {
              wx.hideLoading();
              if (res.data.Code == 0) {
                that.onShow();
              }
            }
          })
        }
      }
    })
  },
  onHide:function(){
    // 生命周期函数--监听页面隐藏
 
  },
  onUnload:function(){
    // 生命周期函数--监听页面卸载
    wx.redirectTo({
          url: "/pages/member/index"
        });
  },
  onPullDownRefresh: function() {
    // 页面相关事件处理函数--监听用户下拉动作
    wx.stopPullDownRefresh();
  },
  onReachBottom: function() {
    // 页面上拉触底事件的处理函数
  
  }
})